package com.lc.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lc.page.PageDomain;
import com.lc.page.TableSupport;
import com.lc.tools.BeanUtils;
import com.lc.tools.StringUtils;
import com.lc.base.entities.TDemo;
import com.lc.base.mapper.TDemoMapper;
import com.lc.base.service.ITDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 测试表 服务实现类
 * </p>
 *
 * @author jsbj
 * @since 2022-03-02
 */
@Service
public class TDemoServiceImpl extends ServiceImpl<TDemoMapper, TDemo> implements ITDemoService {

    @Autowired
    private TDemoMapper tDemoMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public IPage<TDemo> getList(TDemo tDemo) {
        LambdaQueryWrapper<TDemo> queryWrapper = new LambdaQueryWrapper<>();
        String field1 = tDemo.getField1();
        if (StringUtils.isNotEmpty(field1)) {
            queryWrapper.like(TDemo::getField1, field1);
        }
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPage();
        Integer pageSize = pageDomain.getPageSize();
        Page<TDemo> page = new Page<>(pageNum, pageSize);
        IPage<TDemo> pages = tDemoMapper.selectPage(page, queryWrapper);

        return pages;
    }

    @Override
    public List<TDemo> getBycondition(TDemo tDemo) {
        LambdaQueryWrapper<TDemo> queryWrapper = new LambdaQueryWrapper<>();
        String field1 = tDemo.getField1();
        if (StringUtils.isNotEmpty(field1)) {
            queryWrapper.like(TDemo::getField1, field1);
        }
        return tDemoMapper.selectList(queryWrapper);
    }

    @Override
    public Integer addData(TDemo tDemo) {
        int userId = Integer.valueOf(request.getHeader("userId"));
        tDemo.setCreaterId(userId);
        tDemo.setCreateTime(new Date());
        tDemo.setUpdateId(userId);
        return tDemoMapper.insert(tDemo);
    }

    @Override
    public Integer modifyData(TDemo tDemo) {
        int userId = Integer.valueOf(request.getHeader("userId"));
        if (null != tDemo.getId()) {
            TDemo target = tDemoMapper.selectById(tDemo.getId());
            // 更新部分的数据
            BeanUtils.copyPropertiesIgnoreNull(tDemo,target);
            target.setUpdateId(userId);
            tDemoMapper.updateById(target);
        }

        return 1;
    }

    @Override
    public Integer deleteData(Integer id) {
        int userId = Integer.valueOf(request.getHeader("userId"));
        TDemo target = tDemoMapper.selectById(id);
        target.setUpdateId(userId);
        target.setDisabled(1);
        return tDemoMapper.updateById(target);
    }
}
